package com.qire.manhua.activity.helper;

import android.graphics.PointF;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;

import com.qire.common.utils.EquipmentInfoUtil;
import com.qire.manhua.controller.ComicReaderController;
import com.qire.manhua.model.viewModel.impl.ComicReaderViewModel;
import com.qire.manhua.view.comicReader.LoopComicReaderView;

/**
 * 漫画阅读器Touch事件扩展助手
 */
public class ComicReaderTouchHelper {

    private final ComicReaderViewModel viewModel;
    private final ComicReaderController controller;


    private int mTouchSlopSquare;

    public ComicReaderTouchHelper(ComicReaderViewModel viewModel, ComicReaderController controller) {
        this.viewModel = viewModel;
        this.controller = controller;
        this.mTouchSlopSquare = EquipmentInfoUtil.getTouchSlopSquare();

        viewModel.setComicReaderOnTouch((View view, MotionEvent event) -> {
            if(!(view instanceof LoopComicReaderView)) {
                return false;
            }
            LoopComicReaderView loopComicReaderView = (LoopComicReaderView) view;
            // fling 状态不要响应跳页和toggle菜单
            if(loopComicReaderView.getTouchMode() == LoopComicReaderView.TOUCH_MODE_FLING)
                return false;
            boolean handle = dispatchTouchEvent(view,event);
            return handle;
        });

    }

    private static final int screenWidth    = EquipmentInfoUtil.getScreenRect().width();
    private static final int screenHeight   = EquipmentInfoUtil.getScreenRect().height();
//    private static final RectF leftRect     = new RectF(0,0,screenWidth/3,screenHeight);
    private static final RectF topRect      = new RectF(0,0,screenWidth,screenHeight/3);
//    private static final RectF rightRect    = new RectF((screenWidth*2)/3,0,screenWidth,screenHeight);
    private static final RectF bottomRect   = new RectF(0,(screenHeight*2)/3,screenWidth,screenHeight);
//    private static final RectF centreRect   = new RectF(screenWidth/3,screenHeight/3,(screenWidth*2)/3,(screenHeight*2)/3);
    private static final RectF centreRect   = new RectF(0,screenHeight/3,screenWidth,(screenHeight*2)/3);
    private PointF downPoint;
    private boolean isLongPress = false;
    private static final int LONG_PRESS = 2;
    private static final int LONG_PRESS_TIMEOUT = 500;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case LONG_PRESS:
                    controller.comicReaderViewOnLongClick(null);
                    isLongPress = true;
                    break;
                default:
                    throw new RuntimeException("Unknown message " + msg);
            }
        }
    };
    public boolean dispatchTouchEvent(View view, MotionEvent event) {
        final int action = event.getAction();
        switch (action){
            case MotionEvent.ACTION_DOWN:{
                downPoint = new PointF(event.getRawX(),event.getRawY());
                mHandler.removeMessages(LONG_PRESS);
                mHandler.sendEmptyMessageAtTime(LONG_PRESS, MotionEvent.obtain(event).getDownTime() + LONG_PRESS_TIMEOUT);
                isLongPress = false;
                break;
            }
            case MotionEvent.ACTION_MOVE:{

                float distanceX = downPoint.x - event.getRawX();
                float distanceY = downPoint.y - event.getRawY();
                boolean isClick = isClick(distanceX,distanceY);
                if(!isClick) {
                    mHandler.removeMessages(LONG_PRESS);
                    viewModel.foldingMenu();
                }

                if(isLongPress) {
                    return true;
                }

                break;
            }
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP : {
                if(isLongPress) {
//                    loopComicReaderView.goPlayBulletScreen();
                    return true;
                }

                mHandler.removeMessages(LONG_PRESS);

                float distanceX = downPoint.x - event.getRawX();
                float distanceY = downPoint.y - event.getRawY();
                boolean isClick = isClick(distanceX,distanceY);

                if(isClick) {
                    if(centreRect.contains(event.getRawX(),event.getRawY())){
                        viewModel.toggleFoldingMenu();
                    }else if(/*leftRect.contains(event.getRawX(),event.getRawY()) || */topRect.contains(event.getRawX(),event.getRawY())){
//                        loopComicReaderView.smoothScrollByOffset(-EquipmentInfoUtil.getScreenHeight()*2/5);
                        viewModel.smoothScrollComicReader(-EquipmentInfoUtil.getScreenHeight()*2/5);
                        viewModel.foldingMenu();
                    }else if(/*rightRect.contains(event.getRawX(),event.getRawY()) || */bottomRect.contains(event.getRawX(),event.getRawY())){
//                        loopComicReaderView.smoothScrollByOffset(EquipmentInfoUtil.getScreenHeight()*2/5);
                        viewModel.smoothScrollComicReader(EquipmentInfoUtil.getScreenHeight()*2/5);
                        viewModel.foldingMenu();
                    }
                }
                break;
            }
        }
        return false;
    }

    private boolean isClick(float distanceX,float distanceY){
        float adbDistanceX = Math.abs(distanceX);
        float adbDistanceY = Math.abs(distanceY);
        return (adbDistanceX*adbDistanceX) + (adbDistanceY * adbDistanceY) < mTouchSlopSquare;
    }


}
